package com.guigu.qingcheng.servicegoods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.goods.TbSpec;
import com.guigu.qingcheng.servicegoods.service.TbSpecService;
import com.guigu.qingcheng.servicegoods.service.TbTemplateService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/spec")
public class TbSpecController {
    @Autowired
    TbSpecService specService;

    @Autowired
    TbTemplateService templateService;
    //查询所有
    @GetMapping("findAll")
    public R findAll(){
        List<TbSpec> list = this.specService.list(null);
        if (list.size()>0)
            return R.ok().data("list",list);
        return R.error().data("list",list);
    }

    //分页不带条件
    @GetMapping("findPage")
    public R findPage(@RequestParam("page") int page, @RequestParam("size") @PathVariable int size){
        //创建page对象
        Page<TbSpec> pageTbSpec = new Page<>(page,size);
        this.specService.page(pageTbSpec,null);

        long total = pageTbSpec.getTotal();//总记录数
        List<TbSpec> records = pageTbSpec.getRecords(); //数据list集合
        if (total>0 && records.size()>0)
            return R.ok().data("total",total).data("rows",records);
        return R.error();
    }

    //条件查询不带分页
    @PostMapping("findList")
    public R findList(@RequestBody Map<String,Object> searchMap){
        List<TbSpec> list = specService.findList(searchMap);
        if (list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    //条件查询带分页
    @PostMapping("findPage")
    public R pageTbPrefCondition(@RequestBody Map<String, Object> searchMap, @RequestParam("page") int page, @RequestParam("size") int size){
        Page<TbSpec> pageTbSpec = specService.findPage(searchMap,page,size);
        if (pageTbSpec.getRecords().size()>0){
            return R.ok().data("total",pageTbSpec.getTotal()).data("rows",pageTbSpec.getRecords());
        }
        return R.error().data("total",pageTbSpec.getTotal()).data("rows",pageTbSpec.getRecords());
    }

    //根据id查询
    @GetMapping("findById")
    public R findById(@RequestParam("id") int id){
        TbSpec tbSpec = new TbSpec();
        tbSpec = specService.getById(id);
        return R.ok().data("spec",tbSpec);
    }

    //增加数据
    @PostMapping("add")
    public R addTbSpec(@RequestBody TbSpec tbSpec){
        Boolean flag = this.specService.addSpec(tbSpec);
        if (flag)
            return R.ok();
        return R.error();
    }
    //修改数据
    @PostMapping("update")
    public R updateTbSpec(@RequestBody TbSpec tbSpec){
        boolean flag = this.specService.updateById(tbSpec);
        if (flag)
            return R.ok();
        return R.error();
    }

    //删除数据
    @GetMapping("delete")
    public R deleteTbSpecById(@RequestParam("id") int id){
        boolean flag =this.specService.deleteSpec(id);
        if (flag)
            return R.ok();
        return R.error();
    }
}

